{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check Distances Between Same Letters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkDistances"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查相同字母间的距离"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ，该字符串仅由小写英文字母组成，<code>s</code> 中的每个字母都 <strong>恰好</strong> 出现 <strong>两次</strong> 。另给你一个下标从 <strong>0</strong> 开始、长度为 <code>26</code> 的的整数数组 <code>distance</code> 。</p>\n",
    "\n",
    "<p>字母表中的每个字母按从 <code>0</code> 到 <code>25</code> 依次编号（即，<code>'a' -&gt; 0</code>, <code>'b' -&gt; 1</code>, <code>'c' -&gt; 2</code>, ... , <code>'z' -&gt; 25</code>）。</p>\n",
    "\n",
    "<p>在一个 <strong>匀整</strong> 字符串中，第 <code>i</code> 个字母的两次出现之间的字母数量是 <code>distance[i]</code> 。如果第 <code>i</code> 个字母没有在 <code>s</code> 中出现，那么 <code>distance[i]</code> 可以 <strong>忽略</strong> 。</p>\n",
    "\n",
    "<p>如果 <code>s</code> 是一个 <strong>匀整</strong> 字符串，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"abaccb\", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "- 'a' 在下标 0 和下标 2 处出现，所以满足 distance[0] = 1 。\n",
    "- 'b' 在下标 1 和下标 5 处出现，所以满足 distance[1] = 3 。\n",
    "- 'c' 在下标 3 和下标 4 处出现，所以满足 distance[2] = 0 。\n",
    "注意 distance[3] = 5 ，但是由于 'd' 没有在 s 中出现，可以忽略。\n",
    "因为 s 是一个匀整字符串，返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"aa\", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>\n",
    "- 'a' 在下标 0 和 1 处出现，所以两次出现之间的字母数量为 0 。\n",
    "但是 distance[0] = 1 ，s 不是一个匀整字符串。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= s.length &lt;= 52</code></li>\n",
    "\t<li><code>s</code> 仅由小写英文字母组成</li>\n",
    "\t<li><code>s</code> 中的每个字母恰好出现两次</li>\n",
    "\t<li><code>distance.length == 26</code></li>\n",
    "\t<li><code>0 &lt;= distance[i] &lt;= 50</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-distances-between-same-letters](https://leetcode.cn/problems/check-distances-between-same-letters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-distances-between-same-letters](https://leetcode.cn/problems/check-distances-between-same-letters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abaccb\"\\n[1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]', '\"aa\"\\n[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if s[i]==s[j] and distance[ord(s[i])-ord('a')]!=j-i-1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        checklist = {}\n",
    "        temp = 0\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in checklist.keys():\n",
    "                checklist[s[i]] = i\n",
    "            else:\n",
    "                temp = i - checklist[s[i]] - 1\n",
    "                if temp != distance[ord(s[i]) - ord('a')]:\n",
    "                    return False\n",
    "        \n",
    "        return True\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        checklist = {}\n",
    "        temp = 0\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in checklist.keys():\n",
    "                checklist[s[i]] = i\n",
    "            else:\n",
    "                temp = i - checklist[s[i]] - 1\n",
    "                if temp != distance[ord(s[i]) - ord('a')]:\n",
    "                    return False\n",
    "        \n",
    "        return True\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len): #遍历\n",
    "            for j in range(i + 1, s_len): #i 到j的距离\n",
    "                if s[i] == s[j]: #找到了相同字母的\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1: #距离\n",
    "                        return False\n",
    "                    else:  #这里是剪枝\n",
    "                        break  \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        counter=dict()\n",
    "        n=len(s)\n",
    "        for i in range(n):\n",
    "            counter[s[i]]=counter.get(s[i],[])+[i]\n",
    "        for key in counter:\n",
    "            if counter[key][1]-counter[key][0]-1!=distance[ord(key)-ord('a')]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for d, c in zip(distance, ascii_lowercase):\n",
    "            i = s.find(c)\n",
    "            if i == -1:\n",
    "                continue\n",
    "            if i+d+1 >= n:\n",
    "                return False\n",
    "            if s[i+d+1] != c:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        firstIndex = [0] * 26\n",
    "        for i in range(n):\n",
    "            idx = ord(s[i]) - ord('a')\n",
    "            if firstIndex[idx] and i - firstIndex[idx] != distance[idx]:\n",
    "                return False\n",
    "            firstIndex[idx] = i + 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        alphabet = list(map(lambda x:chr(x),range(97,123)))\n",
    "        left = -1\n",
    "        for ch in alphabet:\n",
    "            left += 1\n",
    "            if ch in s:\n",
    "                if s.rfind(ch) - s.find(ch) -1 != distance[left]:\n",
    "                    return False\n",
    "        else:\n",
    "            return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        for i in range(len(s)):\n",
    "            for j in range(i+1, len(s)):\n",
    "                if s[i] == s[j]:\n",
    "                    if distance[ord(s[i])-ord('a')] != j-i-1:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        vals = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in vals:\n",
    "                vals[s[i]] = [i]\n",
    "            else:\n",
    "                vals[s[i]].append(i)\n",
    "        ns = list(set(s))\n",
    "        for i in range(len(ns)):\n",
    "            if vals[ns[i]][1] - vals[ns[i]][0] - 1 != distance[ord(ns[i]) - ord('a')]:\n",
    "                return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dis={}\n",
    "        for i,v in enumerate(s):\n",
    "            if v in dis:\n",
    "                index=ord(v)-97\n",
    "                if distance[index]!=(i-dis[v]-1):\n",
    "                    return False\n",
    "            else:\n",
    "                dis[v]=i\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if s[i] == s[j] and distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        m = {}\n",
    "        for i in range(len(s)):\n",
    "            index = ord(s[i]) - 97\n",
    "            if index not in m.keys():\n",
    "                m[index] = [i]\n",
    "            else:\n",
    "                m[index].append(i)\n",
    "        # print(m)\n",
    "        for i, d in enumerate(distance):\n",
    "            if i not in m.keys():\n",
    "                continue\n",
    "            pos = m[i]\n",
    "            if abs(pos[1]-pos[0]-1) != d:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for d, c in zip(distance, ascii_lowercase):\n",
    "            i = s.find(c)\n",
    "            if i == -1:\n",
    "                continue\n",
    "            if i+d+1 >= n:\n",
    "                return False\n",
    "            if s[i+d+1] != c:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        for i in range(len(s)-1):\n",
    "            for j in range(i+1,len(s)):\n",
    "                if s[j]==s[i]:\n",
    "                    if (j-i-1)!=distance[ord(s[i])-ord(\"a\")]:\n",
    "                        return False\n",
    "                    break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dic = {}\n",
    "        for i, v in enumerate(s):\n",
    "            if v not in dic:\n",
    "                dic[v] = i\n",
    "        for j in dic.keys():\n",
    "            nextIndex = dic[j] + distance[ord(j) - ord('a')] + 1\n",
    "            if nextIndex >= len(s) or s[dic[j]] != s[nextIndex]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            if s[i] not in res:\n",
    "                pos = ord(s[i]) - ord('a')\n",
    "                d = distance[pos]\n",
    "                if i+ d + 1 >= n or  s[i+ d + 1] != s[i] :\n",
    "                    return False\n",
    "                res.append(s[i])\n",
    "                if len(res) == n / 2:\n",
    "                    break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n=len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if s[i] == s[j] and distance[ord(s[i])-ord('a')] != j-i-1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if s[i] == s[j] and distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        flag = {}\n",
    "        for i in range(n):\n",
    "            if s[i] not in flag.keys():\n",
    "                flag[s[i]] = i\n",
    "            elif i-flag[s[i]] != distance[ord(s[i])-ord('a')]+1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        alphabet = list(map(lambda x:chr(x),range(97,123)))\n",
    "        left = -1\n",
    "        for ch in alphabet:\n",
    "            left += 1\n",
    "            if ch in s:\n",
    "                if s.rfind(ch) - s.find(ch) -1 != distance[left]:\n",
    "                    return False\n",
    "        else:\n",
    "            return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        a={}\n",
    "        for i,j in enumerate(s):\n",
    "            if j not in a:\n",
    "                a[j]=[i]\n",
    "            else:\n",
    "                a[j].append(i)\n",
    "        for i in a.keys():\n",
    "            if distance[ord(i)-97]!=a[i][1]-a[i][0]-1:\n",
    "                return False\n",
    "                break\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j]:\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                        return False\n",
    "                    else:\n",
    "                        break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        pre = {}\n",
    "        for i, c in enumerate(s):\n",
    "            if c not in pre:\n",
    "                pre[c] = i\n",
    "            else:\n",
    "                diff = i - pre[c] - 1\n",
    "                if diff != distance[ord(c)-ord('a')]: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_dic = {}\n",
    "        for idx,tmp_s in enumerate(list(s)):\n",
    "            if s_dic.get(tmp_s) != None:\n",
    "                s_dic[ tmp_s ] = (idx - 1 - s_dic[ tmp_s ])\n",
    "            else:\n",
    "                s_dic[ tmp_s ] = idx\n",
    "        for i in s_dic.keys():\n",
    "            if s_dic[i] == distance[ord(i)- ord('a')]:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dic = {}\n",
    "        for i, ch in enumerate(s):\n",
    "            if ch not in dic:\n",
    "                dic[ch] = i\n",
    "            elif i - dic[ch] - 1 != distance[ord(ch) - ord('a')]:\n",
    "                    return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j]:\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                        return False\n",
    "                    else:\n",
    "                        break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n=len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if s[i]==s[j] and distance[ord(s[i])-ord('a')]!=j-i-1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        chars = set()\n",
    "        for i in range(n):\n",
    "            if s[i] not in chars:\n",
    "                j = i + 1 + distance[ord(s[i]) - ord('a')]\n",
    "                if j >= n or s[i] != s[j]:\n",
    "                    return False\n",
    "                chars.add(s[i])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len=len(s)\n",
    "        for i in range(s_len):\n",
    "            d = distance[ord(s[i])-ord('a')]\n",
    "            if d!=-1 and (d+i+1>=s_len or s[d+i+1]!=s[i]):\n",
    "                return False\n",
    "            distance[ord(s[i])-ord('a')] = -1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        if len(s) >100:\n",
    "            return True\n",
    "\n",
    "        dis = [-1]*26\n",
    "\n",
    "        for i, ch in enumerate(s):\n",
    "            index = ord(ch) - ord(\"a\")\n",
    "            dis [index] = i if dis[index]==-1 else i-dis[index]-1\n",
    "        for i, n in enumerate(distance):\n",
    "            if dis[i] == -1:\n",
    "                continue\n",
    "            if dis[i] != n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dct = defaultdict(list)\n",
    "        for i, w in enumerate(s):\n",
    "            dct[w].append(i)\n",
    "        for w in dct:\n",
    "            i = ord(w) - ord(\"a\")\n",
    "            if dct[w][-1]-dct[w][0]-1 != distance[i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        for i in range(26):\n",
    "            c = chr(ord('a') + i)\n",
    "            l = []\n",
    "            for j, ch in enumerate(s):\n",
    "                if ch == c:\n",
    "                    l.append(j)\n",
    "            if not l:\n",
    "                continue\n",
    "            if l[1] - l[0] - 1 != distance[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        flag = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in flag:\n",
    "                if i + 1 + distance[ord(s[i]) - 97] >= len(s):\n",
    "                    return False\n",
    "                if s[i] != s[i + distance[ord(s[i]) - 97] + 1]:\n",
    "                    return False\n",
    "                flag.append(s[i])\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dis=[0]*26\n",
    "        for i in range(len(s)):\n",
    "            for j in range(i+1,len(s)):\n",
    "                if s[i]==s[j] and distance[ord(s[i])-ord('a')]!=j-i-1:\n",
    "                    return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        chars = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in chars.keys():\n",
    "                chars[s[i]] = i\n",
    "            else:\n",
    "                chars[s[i]] = i - chars[s[i]] -1\n",
    "        for index in range(26):\n",
    "            if chr(ord('a')+index) in chars.keys():\n",
    "                if distance[index] == chars[chr(ord('a')+index)]:\n",
    "                    continue\n",
    "                else:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        for i in range(len(s)):\n",
    "            if distance[ord(s[i]) - 97] != -1:\n",
    "                if i + 1 + distance[ord(s[i]) - 97] >= len(s):\n",
    "                    return False\n",
    "                if s[i] != s[i + distance[ord(s[i]) - 97] + 1]:\n",
    "                    return False\n",
    "                distance[ord(s[i]) - 97] = -1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dic = {}\n",
    "        for i in range(len(s)):\n",
    "            dic[s[i]] = dic.get(s[i], []) + [i]\n",
    "        return all(distance[ord(k) - 97] == v[1] - v[0] - 1 for k, v in dic.items())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dis = [-1]*26\n",
    "\n",
    "        for i, ch in enumerate(s):\n",
    "            index = ord(ch) - ord(\"a\")\n",
    "            dis [index] = i if dis[index]==-1 else i-dis[index]-1\n",
    "        for i, n in enumerate(distance):\n",
    "            if dis[i] == -1:\n",
    "                continue\n",
    "            if dis[i] != n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len): #遍历\n",
    "            for j in range(i + 1, s_len): #i 到j的距离\n",
    "                if s[i] == s[j]: #找到了相同字母的\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1: #距离\n",
    "                        return False\n",
    "                    else:  #这里是剪枝\n",
    "                        break  \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        length = len(s)\n",
    "        std = ord('a')\n",
    "        visited = set()\n",
    "        for i, c in enumerate(s):\n",
    "            if c in visited:\n",
    "                continue\n",
    "            new_p = i + distance[ord(c) - std] + 1\n",
    "            if new_p < length and s[new_p] == c:\n",
    "                visited.add(c)\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        letters = string.ascii_lowercase\n",
    "        letters_dist = {}\n",
    "        for i in range(len(distance)):\n",
    "            letters_dist[letters[i]] = distance[i]\n",
    "        \n",
    "        visited = {}\n",
    "        \n",
    "       \n",
    "        for i in range(len(s)):\n",
    "            j = i + 1\n",
    "            if i in visited:\n",
    "                continue\n",
    "            while j<len(s) and s[j] != s[i]:\n",
    "                j += 1\n",
    "\n",
    "            visited[j] = None\n",
    "            \n",
    "            this_distance = j - i - 1\n",
    "            if this_distance != letters_dist[s[i]]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j]:\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                        return False\n",
    "                    else:\n",
    "                        break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for d, c in zip(distance, ascii_lowercase):\n",
    "            i = s.find(c)\n",
    "            if i == -1:\n",
    "                continue\n",
    "            if i+d+1 >= n:\n",
    "                return False\n",
    "            if s[i+d+1] != c:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        letters = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        d = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}\n",
    "        for letter in set(s):\n",
    "            l1 = s.find(letter)\n",
    "            l2 = s.rfind(letter)\n",
    "            if l2 - l1 - 1 != distance[d[letter]]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if s[i] == s[j] and distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j] and distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        distance_dict = collections.defaultdict(list)\n",
    "        for i in range(len(s)):\n",
    "            distance_dict[s[i]].append(i)\n",
    "        \n",
    "        for key, value in distance_dict.items():\n",
    "            if distance[ord(key) - ord('a')] != value[1] - value[0] - 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        length = len(s)\n",
    "        std = ord('a')\n",
    "        flag = [0] * length\n",
    "        for i, c in enumerate(s):\n",
    "            if flag[i] == 1:\n",
    "                continue\n",
    "            new_p = i + distance[ord(c) - std] + 1\n",
    "            if new_p < length and s[new_p] == c:\n",
    "                flag[new_p] = 1\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "      checked = {}\n",
    "      for i,x in enumerate(s):\n",
    "        if x in checked:\n",
    "          continue\n",
    "        dist = distance['abcdefghijklmnopqrstuvwxyz'.index(x)]\n",
    "        j = i+dist+1\n",
    "        try:\n",
    "          if s[j] ==x:\n",
    "            checked[x] = True\n",
    "          else:\n",
    "            return False\n",
    "        except:\n",
    "          return False\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            if s[i] not in res:\n",
    "                pos = ord(s[i]) - ord('a')\n",
    "                d = distance[pos]\n",
    "                if i+ d + 1 >= n or  s[i+ d + 1] != s[i] :\n",
    "                    return False\n",
    "                res.append(s[i])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        base = ord('a')\n",
    "        adic = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in adic:\n",
    "                adic[s[i]].append(i)\n",
    "            else:\n",
    "                adic[s[i]] = [i]\n",
    "        for k,v in adic.items():\n",
    "            i = ord(k)-base\n",
    "            if distance[i] != (v[1]-v[0]-1):\n",
    "                return False\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        d = defaultdict(int)\n",
    "        for i, c in enumerate(s, 1):\n",
    "            if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:\n",
    "                return False\n",
    "            d[c] = i\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        pos = [-1] * 26\n",
    "        for i, ch in enumerate(s):\n",
    "            if pos[ord(ch) - ord('a')] == -1:\n",
    "                pos[ord(ch) - ord('a')] = i\n",
    "            elif i - pos[ord(ch) - ord('a')] - 1 != distance[ord(ch) - ord('a')]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        for w in set(s):\n",
    "            ind = ord(w) - ord('a')\n",
    "            if s.rfind(w) - s.find(w) -1 != distance[ind]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        for i in range(len(s)):\n",
    "            j = ord(s[i]) - ord('a');\n",
    "            if distance[j] >= 0:\n",
    "                distance[j] = -(i + distance[j] + 1)\n",
    "            else:\n",
    "                if i!= -distance[j]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j] and distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dic = {}\n",
    "        for i in range(len(s)):\n",
    "            dic[s[i]] = dic.get(s[i], []) + [i]\n",
    "        print(dic)\n",
    "        for k, v in dic.items():\n",
    "            if distance[ord(k) - 97] != v[1] - v[0] - 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j] and distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for index, ss in enumerate(s):\n",
    "            if ss in d:\n",
    "                d[ss].append(index)\n",
    "            else:\n",
    "                d[ss] = [index]\n",
    "        for key, val in d.items():\n",
    "            if distance[ord(key) - ord('a')] != val[1] - val[0] - 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_dict = {\n",
    "        'a': 0,'b': 1,'c': 2, 'd': 3,'e': 4,'f': 5,'g': 6,\n",
    "        'h': 7,'i': 8,'j': 9, 'k': 10,'l': 11,'m': 12,'n': 13,\n",
    "        'o': 14,'p': 15,'q': 16, 'r': 17,'s': 18,'t':19,'u':20,\n",
    "        'v': 21,'w': 22,'x': 23, 'y': 24,'z': 25\n",
    "        }\n",
    "        distance1=[-1]*26\n",
    "        for i in range (len(s)):\n",
    "            if distance1[s_dict[s[i]]] ==-1:\n",
    "                distance1[s_dict[s[i]]]=i\n",
    "            else:\n",
    "                distance1[s_dict[s[i]]]=i-distance1[s_dict[s[i]]]-1\n",
    "\n",
    "        for i in range(26):\n",
    "            if distance1[i]!=-1:\n",
    "                if distance1[i]!=distance[i]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        Set = set(s)\n",
    "        print(Set)\n",
    "        for i in Set:\n",
    "            if distance[ord(i) - 97] != len(s.split(i)[1]):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        hax = {}\n",
    "        for i, x in enumerate(s):\n",
    "            num = ord(x) - 97\n",
    "            if num not in hax:\n",
    "                hax[num] = i\n",
    "            else:\n",
    "                hax[num] = i - hax[num]-1\n",
    "                if hax[num] != distance[num]:\n",
    "                    return False\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        d = dict()\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d:\n",
    "                d[s[i]] = i\n",
    "            else:\n",
    "                tem = i-d[s[i]]-1\n",
    "                if distance[ord(s[i])-97] != tem:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        dis = [-1]*26\n",
    "\n",
    "        for i, ch in enumerate(s):\n",
    "            index = ord(ch) - ord(\"a\")\n",
    "            dis [index] = i if dis[index]==-1 else i-dis[index]-1\n",
    "        for i, n in enumerate(distance):\n",
    "            if dis[i] == -1:\n",
    "                continue\n",
    "            if dis[i] != n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        d1={}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d1:\n",
    "                d1[s[i]]=[i]\n",
    "            else:\n",
    "                d1[s[i]].append(i)\n",
    "        for i in range(26):\n",
    "            if chr(i+97) in d1 and distance[i] != d1[chr(i+97)][1]-d1[chr(i+97)][0]-1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j]:\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                        return False\n",
    "                    else:\n",
    "                        break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        str_dict = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in str_dict.keys():\n",
    "                dis = i - str_dict[s[i]] - 1\n",
    "                index = ord(s[i]) - ord('a')\n",
    "                if dis != distance[index]:\n",
    "                    return False\n",
    "            else:\n",
    "                str_dict[s[i]] = i\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        ans = []\n",
    "        for a,i in enumerate(s) :\n",
    "            if i not in ans:\n",
    "                #print(ord(i))\n",
    "                #print(s[int(distance[ord(i) - 97]) + a])\n",
    "                if distance[ord(i) - 97] + a +1> len(s) - 1:\n",
    "                    return False\n",
    "                if s[distance[ord(i) - 97] + a +1] != i:\n",
    "                    return False\n",
    "                else:\n",
    "                    ans.append(i)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        s_len = len(s)\n",
    "        for i in range(s_len):\n",
    "            for j in range(i + 1, s_len):\n",
    "                if s[i] == s[j]:\n",
    "                    if distance[ord(s[i]) - ord('a')] != j - i - 1:\n",
    "                        return False\n",
    "                    else:\n",
    "                        break\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        last = [0] * 26\n",
    "        for i, c in enumerate(s):\n",
    "            c = ord(c) - ord('a')\n",
    "            if last[c] and i - last[c] != distance[c]:\n",
    "                return False\n",
    "            last[c] = i + 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        firstIndex = [0] * 26;\n",
    "        for i in range(n):\n",
    "            idx = ord(s[i]) - ord('a');\n",
    "            if firstIndex[idx] and i - firstIndex[idx] != distance[idx]:\n",
    "                return False\n",
    "            firstIndex[idx] = i + 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        # 初始化一个字典来存储每个字母的首次和最后一次出现的索引\n",
    "        letter_indices = {}\n",
    "        \n",
    "        # 遍历字符串，记录每个字母的首次和最后一次出现的索引\n",
    "        for i, letter in enumerate(s):\n",
    "            if letter not in letter_indices:\n",
    "                letter_indices[letter] = [i, i]\n",
    "            else:\n",
    "                letter_indices[letter][1] = i\n",
    "        \n",
    "        # 检查每个字母的实际距离是否符合 distance 中的距离\n",
    "        for letter, indices in letter_indices.items():\n",
    "            # 计算实际的内部距离\n",
    "            actual_distance = indices[1] - indices[0] - 1\n",
    "            # 获取字母对应的 distance 数组中的索引（'a' -> 0, 'b' -> 1, ... 'z' -> 25）\n",
    "            letter_index = ord(letter) - ord('a')\n",
    "            # 如果实际距离与 distance 数组中的距离不符，返回 False\n",
    "            if distance[letter_index] != actual_distance:\n",
    "                return False\n",
    "\n",
    "        # 如果所有字母的距离都符合 distance 数组中的定义，返回 True\n",
    "        return True\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# from collections import defaultdict\n",
    "# class Solution:\n",
    "#     def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "#         dic=defaultdict(list)\n",
    "#         for i,ch in enumerate(s):\n",
    "#             dic[ch].append(i)   \n",
    "#         for k,v in dic.items():\n",
    "#             if len(v)!=2:\n",
    "#                 return False   \n",
    "#             idx=ord(k)-ord(\"a\")\n",
    "#             value=v[1]-1-v[0]\n",
    "#             if distance[idx]!=value:\n",
    "#                 return False \n",
    "#         return True\n",
    "\n",
    "### 官2：模拟（推荐）\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        n = len(s)\n",
    "        firstIndex = [0] * 26; ### 记录字母曾经出现过的第一次位置索引\n",
    "        for i in range(n):\n",
    "            idx = ord(s[i]) - ord('a');\n",
    "            if firstIndex[idx] and i - firstIndex[idx] != distance[idx]:\n",
    "                return False\n",
    "            firstIndex[idx] = i + 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        # 初始化一个字典来存储每个字母的首次和最后一次出现的索引\n",
    "        letter_indices = {}\n",
    "        \n",
    "        # 遍历字符串，记录每个字母的首次和最后一次出现的索引\n",
    "        for i, letter in enumerate(s):\n",
    "            if letter not in letter_indices:\n",
    "                letter_indices[letter] = [i, i]\n",
    "            else:\n",
    "                letter_indices[letter][1] = i\n",
    "        \n",
    "        # 检查每个字母的实际距离是否符合 distance 中的距离\n",
    "        for letter, indices in letter_indices.items():\n",
    "            # 计算实际的内部距离\n",
    "            actual_distance = indices[1] - indices[0] - 1\n",
    "            # 获取字母对应的 distance 数组中的索引（'a' -> 0, 'b' -> 1, ... 'z' -> 25）\n",
    "            letter_index = ord(letter) - ord('a')\n",
    "            # 如果实际距离与 distance 数组中的距离不符，返回 False\n",
    "            if distance[letter_index] != actual_distance:\n",
    "                return False\n",
    "\n",
    "        # 如果所有字母的距离都符合 distance 数组中的定义，返回 True\n",
    "        return True\n",
    "\n",
    "# 示例使用\n",
    "solution = Solution()\n",
    "s = \"abac\"\n",
    "distance = [1] * 26\n",
    "print(solution.checkDistances(s, distance))  # 应返回 True 或 False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        ma={}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in ma:\n",
    "                ma[s[i]] =i\n",
    "            else:\n",
    "                dis=i-ma[s[i]]-1\n",
    "                if distance[ord(s[i])-ord(\"a\")]!=dis:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def checkDistances(self, s: str, distance: List[int]) -> bool:\n",
    "        # 初始化一个字典来存储每个字母的首次和最后一次出现的索引\n",
    "        letter_indices = {}\n",
    "        \n",
    "        # 遍历字符串，记录每个字母的首次和最后一次出现的索引\n",
    "        for i, letter in enumerate(s):\n",
    "            if letter not in letter_indices:\n",
    "                letter_indices[letter] = [i, i]\n",
    "            else:\n",
    "                letter_indices[letter][1] = i\n",
    "        \n",
    "        # 检查每个字母的实际距离是否符合 distance 中的距离\n",
    "        for letter, indices in letter_indices.items():\n",
    "            # 计算实际的内部距离\n",
    "            actual_distance = indices[1] - indices[0] - 1\n",
    "            # 获取字母对应的 distance 数组中的索引（'a' -> 0, 'b' -> 1, ... 'z' -> 25）\n",
    "            letter_index = ord(letter) - ord('a')\n",
    "            # 如果实际距离与 distance 数组中的距离不符，返回 False\n",
    "            if distance[letter_index] != actual_distance:\n",
    "                return False\n",
    "\n",
    "        # 如果所有字母的距离都符合 distance 数组中的定义，返回 True\n",
    "        return True\n",
    "\n",
    "# 示例使用\n",
    "solution = Solution()\n",
    "s = \"abac\"\n",
    "distance = [1] * 26\n",
    "print(solution.checkDistances(s, distance))  # 应返回 True 或 False\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
